package com.smh.a05PokerGameDemo2;

import java.util.*;

public class PokerGame2 {

    static ArrayList<String> list = new ArrayList<>();
    static HashMap<String,Integer> hm = new HashMap<>();

    static {

        ArrayList<String> color = new ArrayList<>();
        Collections.addAll(color,"♥","♠","♦","♣");
        ArrayList<String> number = new ArrayList<>();
        Collections.addAll(number,"3","4","5","6","7","8","9","10","J","Q","K","A","2");
        for (String n : number) {
            for (String c : color) {
                list.add(c+n);
            }
        }
        list.add("小王");
        list.add("大王");
        System.out.println(list);

        //指定牌的价值
        //牌上的数字到map集合中判断是否存在
        //存在,获取价值
        //不存在,本身数据就是价值
        hm.put("J",11);
        hm.put("Q",12);
        hm.put("K",13);
        hm.put("A",14);
        hm.put("2",15);
        hm.put("小王",16);
        hm.put("大王",17);

    }


    public PokerGame2(){
        //打乱
        Collections.shuffle(list);
        //发牌
        //地主()
        ArrayList<String> lord =    new ArrayList<>();
        ArrayList<String> player1 = new ArrayList<>();
        ArrayList<String> player2 = new ArrayList<>();
        ArrayList<String> player3 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (i<3){
                lord.add(list.get(i));
                continue;
            }

            if (i%3==0){

                player1.add(list.get(i));
            } else if (i%3==1) {

                player2.add(list.get(i));
            }else {

                player3.add(list.get(i));
            }
        }
        System.out.println(lord);
        System.out.println(player1);
        System.out.println(player2);
        System.out.println(player3);

        //排序
        order(lord);
        order(player1);
        order(player2);
        order(player3);

        //看牌
        lookPoker("zhangsan",player1);
        lookPoker("lisi",player2);
        lookPoker("wangwu",player3);


    }
    public void order(ArrayList<String> list){
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int i = getValue(o1)-getValue(o2);
                String color1 = o1.substring(0,1);
                String color2 = o2.substring(0,1);
                /*if (i==0){
                    i= color1.compareTo(color2);
                }*/
                //价值一样,比较花色
                return i==0?color1.compareTo(color2):i;
            }
        });
    }
    public int getValue(String s){
        //大小王
        if (hm.containsKey(s)){
            return hm.get(s);
        }
        //J到2
        String number = s.substring(1);
        if (hm.containsKey(number)){
            return hm.get(number);
        }
        //3-10
        int value = Integer.valueOf(number);
        return value;
    }

    public void lookPoker(String name,ArrayList<String> list){
        System.out.print(name+": ");
        StringJoiner sj =new StringJoiner(",");
        for (String s : list) {
            sj.add(s);
        }
        System.out.println(sj);
    }
}
